package com.tn.service.rpm.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tn.controller.alarm.common.constants.RpmConstants;
import com.tn.controller.alarm.common.enums.RpmEventTypeEnum;
import com.tn.controller.alarm.common.model.rpm.*;
import com.tn.dao.rpm.IEventLogDAO;
import com.tn.service.rpm.RpmServeService;
import com.tn.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName RmpServeServiceImpl
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2021/12/24 16:29
 * @Version v1.0
 **/
@Service
@Slf4j
public class RpmServeServiceImpl implements RpmServeService {
    @Autowired
    private RpmHttpRequest httpRequest;
    @Autowired
    private IEventLogDAO iEventLogDAO;
    @Override
    public int sendStaticFenceCheck(StaticFenceCheckParamDTO staticFenceCheckParamDTO) {
        RpmRequestVO<StaticFenceCheckParamDTO> rmpRequestVO = buildParam(staticFenceCheckParamDTO,RpmConstants.RMPACTIONTYPE.CREATE.getType(),
                RpmConstants.RMPTYPE.STATICFENCECHECK.getType(),false, RpmEventTypeEnum.STATICFENCE.getCode(),0);
        httpRequest.rmpAsync(rmpRequestVO);
        return rmpRequestVO.getId();
    }

    @Override
    public void deleteStaticFenceCheck(int eventId) {
        RpmRequestVO rmpRequestVO = baseBuildParam(null,RpmConstants.RMPACTIONTYPE.DELETE.getType(),
                RpmConstants.RMPTYPE.STATICFENCECHECK.getType(),false);
        rmpRequestVO.setId(eventId);
        httpRequest.rmpAsync(rmpRequestVO);
    }

    @Override
    public void sendInRadiusCheck(InRadiusCheckParamDTO inRadiusCheckParamDTO, String actionType) {
        RpmRequestVO<InRadiusCheckParamDTO> rmpRequestVO = buildParam1(inRadiusCheckParamDTO, actionType,
                RpmConstants.RMPTYPE.TOFDISCHECK.getType(),false, RpmEventTypeEnum.RADIUS.getCode(),0);
        System.out.println(rmpRequestVO);
        httpRequest.rmpAsync(rmpRequestVO);
    }

    @Override
    public Map<String,String[]> queryTraceFetch(String tagId, String startTime, String endTime) {
        TraceFetchParamDTO traceFetchParamDTO = TraceFetchParamDTO.builder().tagid(tagId).starttime(startTime).endtime(endTime).build();
        RpmRequestVO<InRadiusCheckParamDTO> rmpRequestVO = baseBuildParam(traceFetchParamDTO, RpmConstants.RMPACTIONTYPE.CREATE.getType(),RpmConstants.RMPTYPE.TRACEFETCH.getType(),true);
        return httpRequest.rmpSync(rmpRequestVO).getResult();
    }

    @Override
    public void sendAddFrontendDisplay(Integer id,String tagId, String jsonData) {
        Map<String,Map<String,String>> map = new HashMap<>();
        Map<String,String> sonMap = new HashMap<>();
        sonMap.put(tagId,jsonData);
        map.put("taginfo",sonMap);
        RpmRequestVO<Map<String,Map<String,String>>> rmpRequestVO = baseBuildParamMap(map, RpmConstants.RMPACTIONTYPE.CREATE.getType(),RpmConstants.RMPTYPE.FRONTENDDISPLAY.getType(),false);
        int requestId = insertEventLog(RpmEventTypeEnum.FRONTENDDISPLAY.getCode(), JSON.toJSONString(rmpRequestVO),id,tagId);
        rmpRequestVO.setId(requestId);
        httpRequest.rmpAsync(rmpRequestVO);
    }

    @Override
    public void sendDeleteFrontendDisplay(String tagId) {
        Map<String,String[]> map = new HashMap<>();
        String[] dataArr = {tagId};
        map.put("deletetags",dataArr);
        RpmRequestVO<Map<String,String[]>> rmpRequestVO = baseBuildParamMap(map, RpmConstants.RMPACTIONTYPE.UPDATE.getType(),RpmConstants.RMPTYPE.FRONTENDDISPLAY.getType(),false);
        int eventId = iEventLogDAO.getIdByTagId(tagId,RpmEventTypeEnum.FRONTENDDISPLAY.getCode());
        if(0==eventId){
            return;
        }
        rmpRequestVO.setId(eventId);
        httpRequest.rmpAsync(rmpRequestVO);
    }

    @Override
    public void sendCameraAreaList(List<CameraAreaListDTO> list) {
        Map<String,List<CameraAreaListDTO>> map = new HashMap<>();
        map.put("arealist",list);
        RpmRequestVO<Map<String,List<CameraAreaListDTO>>> rmpRequestVO = baseBuildParamMap(map, RpmConstants.RMPACTIONTYPE.CREATE.getType(),RpmConstants.RMPTYPE.AREALIST.getType(),false);
        String data = JSON.toJSONString(rmpRequestVO);
        int maxLen = 1024;
        if(data.length() >= maxLen) {
            String postStr = StrUtil.format("...({})", data.length());
            data = data.substring(0, maxLen - 40) + postStr;
        }
        int requestId = insertEventLog(RpmEventTypeEnum.AREALIST.getCode(), data,0,null);
        rmpRequestVO.setId(requestId);
        httpRequest.rmpSync(rmpRequestVO);
    }

    @Override
    public List<CameraAreaListResultVO> areaTraceFetchData(String tagId, String startTime, String endTime) {
        TraceFetchParamDTO traceFetchParamDTO = TraceFetchParamDTO.builder().tagid(tagId).starttime(startTime).endtime(endTime).build();
        RpmRequestVO<InRadiusCheckParamDTO> rmpRequestVO = baseBuildParam(traceFetchParamDTO, RpmConstants.RMPACTIONTYPE.CREATE.getType(),RpmConstants.RMPTYPE.AREATRACEFETCH.getType(),false);
        Integer eventId = iEventLogDAO.getIdByTagId(tagId,RpmEventTypeEnum.AREALIST.getCode());
        if(null == eventId){
            return null;
        }
        return httpRequest.rmpSyncList(rmpRequestVO).getResult();
    }

    private RpmRequestVO baseBuildParam(Object obj, String actionType, String type, boolean sync) {
        Long time = Calendar.getInstance().getTimeInMillis();
        RpmRequestVO rmpRequestVO = new RpmRequestVO<>();
        rmpRequestVO.setAction(actionType);
        rmpRequestVO.setTimestamp(time);
        rmpRequestVO.setType(type);
        rmpRequestVO.setWorkload(obj);
        rmpRequestVO.setSync(sync);
        return rmpRequestVO;
    }

    private RpmRequestVO baseBuildParamMap(Map obj, String actionType, String type, boolean sync) {
        Long time = Calendar.getInstance().getTimeInMillis();
        RpmRequestVO<Map> rmpRequestVO = new RpmRequestVO<>();
        rmpRequestVO.setAction(actionType);
        rmpRequestVO.setTimestamp(time);
        rmpRequestVO.setType(type);
        rmpRequestVO.setWorkload(obj);
        rmpRequestVO.setSync(sync);
        return rmpRequestVO;
    }


    private RpmRequestVO buildParam(Object obj, String actionType, String type, boolean sync,int checkType,int foreignId) {
        RpmRequestVO rmpRequestVO = baseBuildParam(obj,actionType,type,sync);
        int requestId = insertEventLog(checkType, JSON.toJSONString(rmpRequestVO),foreignId,null);
        rmpRequestVO.setId(requestId);
        return rmpRequestVO;
    }
    private RpmRequestVO buildParam1(InRadiusCheckParamDTO obj, String actionType, String type, boolean sync,int checkType,int foreignId) {
        RpmRequestVO rmpRequestVO = baseBuildParam(obj,actionType,type,sync);
        int requestId = insertEventLog(checkType, JSON.toJSONString(rmpRequestVO),foreignId,obj.getCentraltag());
        rmpRequestVO.setId(requestId);
        return rmpRequestVO;
    }

    private int insertEventLog(int checkType,String data,int foreignId,String tagId) {
        RpmEventLogDO rpmEventLogDO = RpmEventLogDO.builder().tagId(tagId).type(checkType).foreignId(foreignId).createTime(DateUtil.dateToString(new Date())).extranData(data).build();
        iEventLogDAO.insert(rpmEventLogDO);
        return rpmEventLogDO.getId();
    }

//    @PostConstruct
//    public void init(){
////        StaticFenceCheckParamDTO staticFenceCheckParamDTO = StaticFenceCheckParamDTO.builder().build();
////        sendStaticFenceCheck(staticFenceCheckParamDTO);
////        InRadiusCheckParamDTO inRadiusCheckParamDTO = InRadiusCheckParamDTO.builder().build();
////        queryTraceFetch("0800121","2022-02-22 12:12:00","2022-02-23 12:12:00");
////        sendAddFrontendDisplay("11233","121212");
//    }
}
